package com.ssyt.hiring.common;

import java.util.List;
import java.util.UUID;

import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.common.util.MailUtil;
import com.ssyt.hiring.talent.service.TalentService;

public class CommonService {

	private TalentService talentService = new TalentService();

	/**
	 * 发送找回密码邮件
	 * 
	 * @param email
	 * @param accountType
	 */
	public boolean send(String email, String accountType) {
		String securityKey = UUID.randomUUID().toString();

		StringBuffer sb = new StringBuffer();
		sb.append("<p>你好 <a href=\"mailto:" + email + "\" target=\"_blank\">"
				+ email + "</a>!</p>");
		sb.append("<p>您已经请求了重置密码，您可以</p>");
		sb.append("<p><a href=\"http://localhost:8081/hiring/CommonServlet?command=toUpdatePasswordUI&email="
				+ email
				+ "&accountType="
				+ accountType
				+ "&securityKey="
				+ securityKey
				+ "\" target=\"_blank\">点此链接重置密码（此链接5分钟内有效）</a></p>");
		sb.append("<p>如果你没有请求重置密码，请忽略这封邮件</p>");
		sb.append("<p>在你点击上面链接修改密码之前，你的密码将会保持不变。</p>");
		boolean sendResult = MailUtil
				.send(email, "Hiring重置密码信息", sb.toString());

		if (sendResult) {
			// 如果邮件发送成功，更新此次邮件的重置密码地址的过期时间
			long millis = System.currentTimeMillis() + (5 * 60 * 1000);
			talentService.updateSecurityKeyAndEmailLinkOutDate(email,
					accountType, millis, securityKey);
		}

		return sendResult;
	}

	/**
	 * 显示所有的省/直辖市
	 * 
	 * @return
	 */
	public List<Province> listAllProvince() {
		return DBUtil.executeQuery(Province.class, "SELECT * FROM province");
	}

	/**
	 * 根据ProvinceId查询对应City
	 * 
	 * @return
	 */
	public List<City> listCityByProvinceId(String provinceId) {
		return DBUtil.executeQuery(City.class,
				"SELECT * FROM city WHERE father=?", provinceId);
	}

	/**
	 * 根据CityId查询对应Area
	 * 
	 * @return
	 */
	public List<Area> listAreaByCityId(String cityId) {
		return DBUtil.executeQuery(Area.class,
				"SELECT * FROM area WHERE father=?", cityId);
	}

}
